package com.atguigu.gulimall.cart.controller;

import com.atguigu.gulimall.cart.entity.CartItemVo;
import com.atguigu.gulimall.cart.entity.CartVo;
import com.atguigu.gulimall.cart.feign.ProductFeignService;
import com.atguigu.gulimall.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private ProductFeignService productFeignService;


    @GetMapping(value = "/currentUserCartItems")
    @ResponseBody
    public List<CartItemVo> getCurrentCartItems() {
        List<CartItemVo> cartItemVoList = cartService.getUserCartItems();
        return cartItemVoList;
    }


    @GetMapping("/itemChecked")
    public String itemChecked(Model model){
        CartVo cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }


    @GetMapping("/cartList")
    public String cartListPage(Model model){
        CartVo cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }


    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes){
        CartItemVo cartItem=cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);

        return "redirect:http://localhost:9020/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCart(@RequestParam("skuId") Long skuId,Model model){
        CartItemVo cartItem=cartService.getCartItem(skuId);
        model.addAttribute("cartItem",cartItem);
        return "success";
    }


    /**
     * 商品是否选中
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping(value = "/checkItem")
    public String checkItem(@RequestParam(value = "skuId") Long skuId,
                            @RequestParam(value = "checked") Integer checked) {

        cartService.checkItem(skuId,checked);

        return "redirect:http://localhost:9020/cartList";

    }


    /**
     * 改变商品数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping(value = "/countItem")
    public String countItem(@RequestParam(value = "skuId") Long skuId,
                            @RequestParam(value = "num") Integer num) {

        cartService.changeItemCount(skuId,num);

        return "redirect:http://localhost:9020/cartList";
    }


    /**
     * 删除商品信息
     * @param skuId
     * @return
     */
    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam("skuId") Integer skuId) {

        cartService.deleteIdCartInfo(skuId);

        return "redirect:http://localhost:9020/cartList";

    }

}
